<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dic extends AdminController {

	function __construct() { 
		parent::__construct();
		$this -> load -> model("Dic_model");
		$this -> load -> library("ProjectService/dic_service");
	}

	public function Index() {
		$data['dic']=$this->dic_service->getWebConfig(array(),false,getSess()['park_id']);
        $data['park_id']=getSess()['park_id'];
		$this -> load -> view('admin/dicList',$data);
	}

	function save(){
		$data=$_POST;
		if (checkStringIsBase64($this->NOTXSS_POST['logo'])) {
            $checkRet = uploadImg($this->NOTXSS_POST['logo'], 'head');
            if (!$checkRet['status']) {
                exit(toRetJson($checkRet));
            }
            $data['logo'] = $checkRet["v"];
        } else {
            $data['logo']=$this->NOTXSS_POST['logo'];
        }
        if (checkStringIsBase64($this->NOTXSS_POST['corporation_img'])) {
            $checkRet = uploadImg($this->NOTXSS_POST['corporation_img'], 'head');
            if (!$checkRet['status']) {
                exit(toRetJson($checkRet));
            }
            $data['corporation_img'] = $checkRet["v"];
        } else {
            $data['corporation_img']=$this->NOTXSS_POST['corporation_img'];
        }
        if (checkStringIsBase64($this->NOTXSS_POST['equipment_img'])) {
            $checkRet = uploadImg($this->NOTXSS_POST['equipment_img'], 'head');
            if (!$checkRet['status']) {
                exit(toRetJson($checkRet));
            }
            $data['equipment_img'] = $checkRet["v"];
        } else {
            $data['equipment_img']=$this->NOTXSS_POST['topic_img'];
        }
        if (checkStringIsBase64($this->NOTXSS_POST['topic_img'])) {
            $checkRet = uploadImg($this->NOTXSS_POST['topic_img'], 'head');
            if (!$checkRet['status']) {
                exit(toRetJson($checkRet));
            }
            $data['topic_img'] = $checkRet["v"];
        } else {
            $data['topic_img']=$this->NOTXSS_POST['topic_img'];
        }
        if (checkStringIsBase64($this->NOTXSS_POST['goods_img'])) {
            $checkRet = uploadImg($this->NOTXSS_POST['goods_img'], 'head');
            if (!$checkRet['status']) {
                exit(toRetJson($checkRet));
            }
            $data['goods_img'] = $checkRet["v"];
        } else {
            $data['goods_img']=$this->NOTXSS_POST['goods_img'];
        }

		if(!isset($data['integral_ranking_show'])){
			$data['integral_ranking_show']=0;
		}
		if(!isset($data['qrcode_apply_ranking_show'])){
			$data['qrcode_apply_ranking_show']=0;
		}
		foreach ($data as $k => $v) {
			$id=$this->Dic_model->get_single(array('dic_key'=>$k));
			$this->Dic_model->update(array('id'=>$id['id'],'dic_value'=>$v));
		}
		exit(retJson('保存成功',true));
	}
	
    function changeDic(){
        $park_id=getSess()['park_id'];
        $dic_key=$_POST['dic_key'];
        $type=(new DicKey)->showByIndex($dic_key,XPHEnum::Remark);
        if(isset($_POST['text'])){
            $sql=$this->sqlEscape(" update db_dic set dic_value=?? where park_id=?? and type=?? and dic_key=?? ",array($_POST['text'],$park_id,$type,$dic_key));
            $this->Dic_model->query($sql,false);
        }
        else if(isset($this->NOTXSS_POST['img'])){
            if (checkStringIsBase64($this->NOTXSS_POST['img'])) {
                $checkRet = uploadImg($this->NOTXSS_POST['img'], UploadType::DicImg[XPHEnum::Remark]);
                if (!$checkRet['status']) {
                    exit(toRetJson($checkRet));
                }
                $img = $checkRet["v"];
            } else {
                $img=$this->NOTXSS_POST['img'];
            }
            $sql=$this->sqlEscape(" update db_dic set dic_value=?? where park_id=?? and type=?? and dic_key=?? ",array($img,$park_id,$type,$dic_key));
            $this->Dic_model->query($sql,false);
        }
        else{
            $dic=$this->dic_service->getWebConfig($dic_key,$type,$park_id);
            if($dic['dic_value']==CommonWhether::Whether[XPHEnum::Value]){
                $dic['dic_value']=CommonWhether::Not[XPHEnum::Value];
                // if($dic_key==DicKey::IntegralRankingShowWeek[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::IntegralRankingShowMonth[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Whether[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
                // else if($dic_key==DicKey::IntegralRankingShowMonth[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::IntegralRankingShowWeek[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Whether[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
                // else if($dic_key==DicKey::QrcodeApplyRankingShowWeek[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::QrcodeApplyRankingShowMonth[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Whether[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
                // else if($dic_key==DicKey::QrcodeApplyRankingShowMonth[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::QrcodeApplyRankingShowWeek[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Whether[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
            }
            else{
                $dic['dic_value']=CommonWhether::Whether[XPHEnum::Value];
                // if($dic_key==DicKey::IntegralRankingShowWeek[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::IntegralRankingShowMonth[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Not[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
                // else if($dic_key==DicKey::IntegralRankingShowMonth[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::IntegralRankingShowWeek[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Not[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
                // else if($dic_key==DicKey::QrcodeApplyRankingShowWeek[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::QrcodeApplyRankingShowMonth[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Not[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
                // else if($dic_key==DicKey::QrcodeApplyRankingShowMonth[XPHEnum::Value]){
                //     $dic_liandong=$this->dic_service->getWebConfig(DicKey::QrcodeApplyRankingShowWeek[XPHEnum::Value],$type,$park_id);
                //     $dic_liandong['dic_value']=CommonWhether::Not[XPHEnum::Value];
                //     $this->Dic_model->update($dic_liandong);
                // }
            }
            $this->Dic_model->update($dic);
        }

        // if($dic_key==DicKey::IntegralRankingShowWeek[XPHEnum::Value]||$dic_key==DicKey::IntegralRankingShowMonth[XPHEnum::Value]||$dic_key==DicKey::QrcodeApplyRankingShowWeek[XPHEnum::Value]||$dic_key==DicKey::QrcodeApplyRankingShowMonth[XPHEnum::Value]){
        //     $v=$this->dic_service->getWebConfig(array(DicKey::IntegralRankingShowWeek[XPHEnum::Value],DicKey::IntegralRankingShowMonth[XPHEnum::Value],DicKey::QrcodeApplyRankingShowWeek[XPHEnum::Value],DicKey::QrcodeApplyRankingShowMonth[XPHEnum::Value]),$type,$park_id);
        //     exit(retJson('保存成功',true,$v));
        // }
        // else{
        //     exit(retJson('保存成功',true));
        // }
        exit(retJson('保存成功',true));
    }
	
}